/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.copier.internal;

import ch.msoftch.internal.*;
import ch.simpleel.accessors.*;
import ch.trackedbean.copier.*;

/**
 * Object holding a flat mapping.
 * 
 * @author M. Hautle
 */
public class SimpleMappingHolder extends AbstractSimpleMappingHolder {
    /** The property converter to use. */
    protected final IPropertyConverter converter;

    /**
     * Default constructor.
     * 
     * @param src The source accessor
     * @param dst The destination accessor (on the object holding the mapping annotations)
     * @param converter The property converter to use or null
     * @param readOnly Flag indicating if the mapping should be 'readonly' so that {@link #copyDst2Src(Object, Object, IMappingContext)} will not be executed
     * @param lazy Flag indicating if the property should be fetched lazy
     * @param profile The profile to which this mapping belongs
     */
    public SimpleMappingHolder(IValueAccessor src, IValueAccessor dst, IPropertyConverter converter, boolean readOnly, boolean lazy, String profile) {
        super(src, dst, readOnly, lazy, profile);
        this.converter = converter != null ? converter : NOPPropertyConverter.NOP;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void copyToDestination(Object src, Object dst, IMappingContext context) {
        try {
            final Object val = srcProp.getValue(src, UNDEFINED);
            if (val != UNDEFINED)
                dstProp.setValue(dst, converter.convertSrc2Dst(val, dstProp.getValue(dst, null), dstProp.getType(), context));
        } catch (Exception e) {
            throw new TechnicalException("Error while copying " + this.srcProp.getPath() + " to " + this.dstProp.getPath(), e);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void copyToSource(Object src, Object dst, IMappingContext context) {
        try {
            final Object val = dstProp.getValue(dst, UNDEFINED);
            // do nothing if there's no way to copy the value
            if (val != UNDEFINED)
                srcProp.setValue(src, converter.convertDst2Src(val, srcProp.getValue(src, null), srcProp.getType(), context));
        } catch (Exception e) {
            throw new TechnicalException("Error while copying " + this.dstProp.getPath() + " to " + this.srcProp.getPath(), e);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public Object getSrcValue(Object src) {
        return converter.convertSrc2Dst(super.getSrcValue(src), null, dstProp.getType(), NOPMappingContext.NOP);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public Object getDstValue(Object dst) {
        return converter.convertDst2Src(super.getDstValue(dst), null, srcProp.getType(), NOPMappingContext.NOP);
    }
}